package com.example.dllo.my36kr.message.enter;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.my.MyUser;
import com.example.dllo.my36kr.tools.BaseAty;
import com.example.dllo.my36kr.tools.StaticUrl;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by dllo on 16/8/29.
 * 头像
 */
public class PicAty extends BaseAty {

    private ImageView imageView;

    @Override
    protected int setLayout() {
        return R.layout.aty_pic;
    }

    @Override
    protected void initView() {
        Button uploadingBnt = bindView(R.id.bnt_aty_uploading);
        imageView = bindView(R.id.iv_aty_pic);
        Button outBnt = bindView(R.id.bnt_pic_out);
        outBnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent intent = new Intent();
                intent.setAction("flush");
                sendBroadcast(intent);
                finish();
            }
        });
        Button quitBnt = bindView(R.id.bnt_pic_tuichu);
        quitBnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("quit");
                sendBroadcast(intent);
                finish();
            }
        });
        uploadingBnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(Intent.ACTION_PICK, null);
                intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent1, 1);


            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        MyUser myUser = new MyUser();
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    cropPhoto(data.getData());// 裁剪图片
                }
                break;
            case 2:
                Bundle extras = data.getExtras();
                Bitmap mBitmap = extras.getParcelable("data");
//                Uri uri = data.getData();
//                ContentResolver cr = this.getContentResolver();
//                try {
//                    mBitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));

                myUser = BmobUser.getCurrentUser(MyUser.class);//上传头像到Bmob
                if (myUser != null) {
                    StaticUrl.createLoadingDialog(PicAty.this);
                    myUser.setIcon(mBitmap);
                    myUser.update(new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null) {
                                StaticUrl.stop();
                                Toast.makeText(PicAty.this, "更新成功", Toast.LENGTH_SHORT).show();
                            } else {
                            }
                        }
                    });
                } else {
                    Toast.makeText(this, "没登录", Toast.LENGTH_SHORT).show();
                }
                imageView.setImageBitmap(mBitmap);
                break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    private void cropPhoto(Uri data) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(data, "image/*");
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 2);
    }



}